
import 'dart:io';

import 'package:courseapp/cache/loadcache.dart';
import 'package:courseapp/entity/SignRecord.dart';
import 'package:courseapp/entity/StudentSignRecordDTO.dart';
import 'package:courseapp/entity/TeacherSignDTO.dart';
import 'package:courseapp/entity/coursedto.dart';
import 'package:courseapp/entity/loaddto.dart';
import 'package:courseapp/entity/loadquery.dart';
import 'package:courseapp/entity/oncecoursedto.dart';
import 'package:courseapp/entity/scoursequery.dart';
import 'package:courseapp/entity/statedto.dart';
import 'package:courseapp/entity/stusigndto.dart';
import 'package:courseapp/entity/stusignquery.dart';
import 'package:courseapp/entity/stusignsumdto.dart';
import 'package:courseapp/page/teacher/index/TeacherTodayCourse.dart';
import 'package:dio/dio.dart';

class HttpUtil {

  static HttpUtil getInstance() {
    return new HttpUtil();
  }

  String baseUrl = "http://127.0.0.1:8080";

  //创建一个dio对象
  Dio createDio() {
    Dio dio = new Dio();
    dio.options.contentType = ContentType.parse("application/x-www-form-urlencoded");
    return dio;
  }

   //发送登陆信息
    Future<LoadDto>  postLogin(LoadQuery loadQuery) async {
    String action = "/course/api/user/load";
    String url = baseUrl + action;
    Response response =  await  createDio().post(url,data:loadQuery.toJson());
    LoadDto loaddto = LoadDto.fromMap(response.data);
    return loaddto;
   }

   Future<Coursedto> postTodayStuCourse() async {
     Studentcourse studentcourse = new Studentcourse();
     studentcourse.stuId = LoadCache.getCache().data.id;
     studentcourse.isAll = false;
     String action = "/course/api/curriculum/query/student/course";
     String url = baseUrl + action;
     Response response = await createDio().post(url,data: studentcourse.toJson());
     Coursedto coursedto = Coursedto.fromMap(response.data);
     return coursedto;
   }

  Future<Coursedto> postAllStuCourse() async {
    Studentcourse studentcourse = new Studentcourse();
    studentcourse.stuId = LoadCache.getCache().data.id;
    studentcourse.isAll = true;
    String action = "/course/api/curriculum/query/student/course";
    String url = baseUrl + action;
    Response response = await createDio().post(url,data: studentcourse.toJson());
    Coursedto coursedto = Coursedto.fromMap(response.data);
    return coursedto;
  }


  Future<Coursedto> postTodayTeacherCourse() async {
    Map json = {"isAll":false,"teacherId":LoadCache.getCache().data.id};
    String action = "/course/api/curriculum/query/teacher/course";
    String url = baseUrl + action;
    Response response = await createDio().post(url,data: json);
    Coursedto coursedto = Coursedto.fromMap(response.data);
    return coursedto;
  }

  Future<Coursedto> postAllTeacherCourse() async {
    Map json = {"isAll":true,"teacherId": LoadCache.getCache().data.id};
    String action = "/course/api/curriculum/query/teacher/course";
    String url = baseUrl + action;
    Response response = await createDio().post(url,data: json);
    Coursedto coursedto = Coursedto.fromMap(response.data);
    return coursedto;

  }







  Future<Stusigndto> postStuSign(int page) async {
    Stusignquery stusignquery = new Stusignquery();
    stusignquery.studentId = LoadCache.getCache().data.id;
    stusignquery.page = page;
    stusignquery.count = 8;
    String action = "/course/api/curriculum/query/sign/course/student/record/multiple";
    String url = baseUrl + action;
    Response response = await createDio().post(url,data: stusignquery.toJson());
    Stusigndto stusigndto = Stusigndto.fromMap(response.data);
    return stusigndto;
  }

 Future<Oncecoursedto> queryCourse(int courseId) async {
    String action = "/course/api/curriculum/query/course/$courseId";
    String url = baseUrl + action;
    Dio dio = new Dio();
    Response response =  await dio.get(url);
    Oncecoursedto oncecoursedto =  Oncecoursedto.fromMap(response.data);
    return oncecoursedto;
 }


Future<Stusignsumdto> queryStuSum(int courseId) async {
    int studentId = LoadCache.getCache().data.id;
    String action = "/course/api/curriculum/query/single/sign/course/student?courseId=$courseId&studentId=$studentId";
    String url = baseUrl + action;
    Dio dio = new Dio();
    Response response = await dio.get(url);
    Stusignsumdto stusignsumdto = Stusignsumdto.fromMap(response.data);
    return stusignsumdto;
}


Future<Statedto> postTeacherSign(int courseId,bool sign) async {
    Map json = {"courseId":courseId,"sign" : sign};
    String action = "/course/api/curriculum/insert/teacher/sign";
    String url = baseUrl + action;
    Response response = await createDio().post(url,data:json);
    Statedto statedto =  Statedto.fromMap(response.data);
    return statedto;
}

Future<SignRecord> queryNewestSign(int courseId) async {
    String action = "/course/api/curriculum/query/single/sign/course/$courseId";
    String url  = baseUrl + action;
    Dio dio = new Dio();
    Response response = await dio.get(url);
    SignRecord signRecord = SignRecord.fromMap(response.data);
    return signRecord;
}


Future<StudentSignRecordDTO> queryStudentSign(int teacherSignId) async {
    String action = "/course/api/curriculum/query/sign/course/teacher/sign/record?teacherSignId=$teacherSignId";
    String url  = baseUrl + action;
    Dio dio = new Dio();
    Response response = await dio.get(url);
    StudentSignRecordDTO signRecordDTO = StudentSignRecordDTO.fromMap(response.data);
    return signRecordDTO;
}

Future<TeacherSignDTO> queryTeacherSign(int courseId) async {
    String action = "/course/api/curriculum/query/multiple/sign/course/$courseId";
    String url  = baseUrl + action;
    Dio dio = new Dio();
    Response response = await dio.get(url);
    print(response.data);
    TeacherSignDTO teacherSignDTO = TeacherSignDTO.fromMap(response.data);
    return teacherSignDTO;
}









}